﻿Imports System.ComponentModel
Imports LSW.Windows.Controls.Forms
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text

Namespace QQ
    ''' <summary>
    ''' CheckBox
    ''' </summary>
    <DefaultEvent("CheckedChanged")> _
    <DefaultProperty("Checked")> _
    <ToolboxBitmap(GetType(CheckBox))> _
    Public Class QQCheckBox
        Inherits CheckBox
#Region "变量"
        ''' <summary>
        ''' 当前的属标状态
        ''' </summary>
        Private _mouseState As EMouseState = EMouseState.Normal

#End Region

#Region "构造函数"
        ''' <summary>
        ''' 
        ''' </summary>
        Public Sub New()
            Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.Selectable Or ControlStyles.UserPaint, True)
            Me.SetStyle(ControlStyles.Opaque, False)
            Me.UpdateStyles()
            Me.BackColor = Color.Transparent
        End Sub

#End Region

#Region "属性"

        ''' <summary>
        ''' 文本区域
        ''' </summary>
        Friend ReadOnly Property TextRect() As Rectangle
            Get
                Return New Rectangle(17, 0, Me.Width - 17, Me.Height)
            End Get
        End Property
        ''' <summary>
        ''' 图片显示区域
        ''' </summary>
        Friend ReadOnly Property ImageRect() As Rectangle
            Get
                Return New Rectangle(0, (Me.Height - 17) / 2, 17, 17)
            End Get
        End Property
        ''' <summary>
        ''' 鼠标状态
        ''' </summary>
        Friend Property MouseState() As EMouseState
            Get
                Return Me._mouseState
            End Get
            Set(value As EMouseState)
                Me._mouseState = value
                MyBase.Invalidate()
            End Set
        End Property

        ''' <summary>
        ''' 重写CheckBox的Text属性
        ''' </summary>
        <DefaultValue("QQCheckBox")> _
        Public Overrides Property Text() As String
            Get
                Return MyBase.Text
            End Get
            Set(value As String)
                MyBase.Text = value
            End Set
        End Property
#End Region

#Region "Override 方法"
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="e"></param>
        Protected Overrides Sub OnPaint(e As PaintEventArgs)
            Dim g As Graphics = e.Graphics
            g.SmoothingMode = SmoothingMode.AntiAlias
            g.TextRenderingHint = TextRenderingHint.AntiAlias

            Dim flags As TextFormatFlags = TextFormatFlags.Left Or TextFormatFlags.SingleLine Or TextFormatFlags.VerticalCenter
            Dim color__1 As Color = If(Me.Enabled, Me.ForeColor, Color.LightGray)
            TextRenderer.DrawText(e.Graphics, Me.Text, Me.Font, Me.TextRect, color__1, flags)
            If Not MyBase.Enabled Then
                Select Case Me.CheckState
                    Case CheckState.Checked
                        Exit Select
                    Case CheckState.Indeterminate
                        Exit Select
                    Case CheckState.Unchecked
                        Dim bitmap As New Bitmap(AssemblyHelper.GetImage("cb_normal.png"))
                        'bitmap = RGB2Gray(bitmap);
                        g.DrawImage(bitmap, Me.ImageRect)
                        Exit Select
                End Select
            Else
                Select Case Me._mouseState
                    Case EMouseState.Normal, EMouseState.Leave
                        If MyBase.Checked Then
                            Using normal As Drawing.Image = AssemblyHelper.GetImage("tick_normal.png")
                                g.DrawImage(normal, Me.ImageRect)
                            End Using
                        Else
                            Using normal As Drawing.Image = AssemblyHelper.GetImage("cb_normal.png")
                                g.DrawImage(normal, Me.ImageRect)
                            End Using
                        End If
                        Exit Select
                    Case EMouseState.Down, EMouseState.Up, EMouseState.Move
                        If MyBase.Checked Then
                            Using high As Drawing.Image = AssemblyHelper.GetImage("tick_highlight.png")
                                g.DrawImage(high, Me.ImageRect)
                            End Using
                        Else
                            Using high As Drawing.Image = AssemblyHelper.GetImage("hightlight.png")
                                g.DrawImage(high, Me.ImageRect)
                            End Using
                        End If
                        Exit Select
                End Select
                If MyBase.CheckState = CheckState.Indeterminate Then
                    If Me.MouseState = EMouseState.Down OrElse Me.MouseState = EMouseState.Move Then
                        Using normal As Drawing.Image = AssemblyHelper.GetImage("_tick_normal.png")
                            g.DrawImage(normal, Me.ImageRect)
                        End Using
                    Else
                        Using high As Drawing.Image = AssemblyHelper.GetImage("_tick_highlight.png")
                            g.DrawImage(high, Me.ImageRect)
                        End Using
                    End If
                End If
            End If
        End Sub
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="eventargs"></param>
        Protected Overrides Sub OnMouseEnter(eventargs As EventArgs)
            MyBase.OnMouseEnter(eventargs)
            Me.MouseState = EMouseState.Move
        End Sub
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="mevent"></param>
        Protected Overrides Sub OnMouseDown(mevent As MouseEventArgs)
            MyBase.OnMouseDown(mevent)
            Me.MouseState = EMouseState.Down
        End Sub
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="eventargs"></param>
        Protected Overrides Sub OnMouseLeave(eventargs As EventArgs)
            MyBase.OnMouseLeave(eventargs)
            Me.MouseState = EMouseState.Leave
        End Sub
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="mevent"></param>
        Protected Overrides Sub OnMouseUp(mevent As MouseEventArgs)
            MyBase.OnMouseUp(mevent)
            Me.MouseState = EMouseState.Up
        End Sub

#End Region
    End Class

End Namespace